package com.axjy.web;

import com.axjy.pojo.PoleInfo;
import com.axjy.service.PoleInfoService;
import com.axjy.service.vo.Pager;
import com.axjy.util.Log;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by l on 2018/1/30.
 */
@RestController
@RequestMapping("/pole")
public class PoleInfoHandler {

    @Resource
    private PoleInfoService poleInfoService;

    @GetMapping("/search")
    public Pager search(@RequestParam(required = false,defaultValue = "0")Integer offset, @RequestParam(required = false,defaultValue = "4")Integer limit, String lineCode, Integer isStart){
        Pager pager=poleInfoService.search(offset, limit, lineCode, isStart);
        return pager;
    }

    @GetMapping("/searchAll")
    public List<PoleInfo> search(){
        return poleInfoService.searchAll();
    }

    @GetMapping("/startPole")
    public List<PoleInfo> startPole(){
        return poleInfoService.startPole();
    }

    @PostMapping("/addPole")
    public String addPole(PoleInfo pole){
        String msg=null;
        try{
            msg="新增成功";
            poleInfoService.addPole(pole);
        }catch (RuntimeException ex){
            msg="新增失败";
            Log.logger.error(msg,ex);
        }
        return msg;
    }

    @PostMapping("/delPole")
    public String delPole(String[] ids){
        String isOk = "1";
        Map<String,Object> map = new HashMap<>();
        map.put("ids",ids);
        try{
            poleInfoService.delPole(map);
        }catch (Exception ex){
            isOk = "0";
        }
        return isOk;
    }

    @PostMapping("/updateStart")
    public String updateStart(String lineCode,Integer isStart){
        String msg=null;
        try{
            msg="修改成功";
            poleInfoService.updateStart(lineCode,isStart);
        }catch (RuntimeException ex){
            msg="修改失败";
            Log.logger.error(msg,ex);
        }
        return msg;
    }

    @PostMapping("/update")
    public String update(PoleInfo poleInfo){
        String msg=null;
        try{
            poleInfoService.update(poleInfo);
            msg="修改成功";
        } catch (RuntimeException ex){
            msg="修改失败";
            Log.logger.error(msg,ex);
        }
        return msg;
    }

    @PostMapping("/updateStatusId")
    public String updateStatusId(Integer isStart, Integer id){
        String msg=null;
        try{
            poleInfoService.updateStatusId(isStart, id);
            msg="1";
        }catch (RuntimeException ex){
            msg="0";
            Log.logger.error(msg,ex);
        }
        return msg;
    }

}
